package cn.imnaer.service.rpc.scenic.process;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.bson.BSONObject;
import org.bson.BasicBSONObject;
import org.bson.types.BasicBSONList;

import cn.imnaer.common.factory.GeoHash;
import cn.imnaer.database.MatchConst;
import cn.imnaer.service.persistence.model.Scenic;
import cn.imnaer.service.persistence.model.base.BaseScenic;
import cn.imnaer.service.persistence.model.consts.SCENIC;
import cn.imnaer.service.rest.resquest.model.Coordinates;
import cn.imnaer.service.rest.view.model.BasicScenic;
import cn.imnaer.service.rest.view.model.Place;
import cn.imnaer.service.rpc.common.AbstractRepository;
import cn.imnaer.service.rpc.common.ICollectCountSerice;
import cn.imnaer.service.rpc.scenic.dao.ScenicDaoImpl;

public class ScenicRepository extends AbstractRepository<Scenic> implements ICollectCountSerice<Scenic> {
	private String scenicId;
	private ScenicDaoImpl scenicDaoImpl = ScenicDaoImpl.getInstance();

	public ScenicRepository(String scenicId) {
		this.scenicId = scenicId;
	}

	public ScenicRepository() {

	}

	// /**
	// * 得到去过这个景点的好友
	// *
	// * @param myUserId
	// * @return
	// */
	// public List<SimpleUser> getScenicBeenHereFriends(
	// String myUserId) {
	// List<SimpleUser> users = new UserRelationModule(myUserId)
	// .getFriendsUsers();
	// HashMap<String, SimpleUser> hash = new HashMap<String, SimpleUser>(
	// users.size());
	// BSONObject query = new BasicBSONObject();
	// BSONObject arr = new BasicBSONList();
	// for (SimpleUser simpleUser : users) {
	// int i = 0;
	// BSONObject matcher = new BasicBSONObject();
	// // matcher.put(TRAVELNOTE.findScenic, this.scenicId);
	// matcher.put(TRAVELNOTE.userId, simpleUser.getUserId());
	// arr.put(String.valueOf(i), matcher);
	// i++;
	// hash.put(simpleUser.getUserId(), simpleUser);
	// }
	// query.put(MatchConst._OR, arr);
	//
	// BSONObject selector;
	// try {
	// selector = BasicBSONObject.typeToBson(new SimpleUser());
	// List<BSONObject> record = TravelNoteDaoImpl.getInstance().getObjects(
	// query, selector);
	// users = new ArrayList<SimpleUser>(record.size());
	// for (BSONObject bsonObject : record) {
	// String userId = bsonObject.as(SimpleUser.class).getUserId();
	// users.add(hash.get(userId));
	// }
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return users;
	// }

	public Scenic getScenicInfo() {
		return scenicDaoImpl.findOneByPk(this.scenicId, Scenic.class);
	}

	public BaseScenic getBaseScenic() {

		return this.scenicDaoImpl.findOneByPk(this.scenicId, BaseScenic.class);

	}

	@Override
	public void updateCollectCount(boolean flag) {
		if (flag) {
			this.scenicDaoImpl.incrementCounterByPk(this.scenicId, SCENIC.collectCount);
		} else {
			this.scenicDaoImpl.decrementCounterByPk(this.scenicId, SCENIC.collectCount);

		}
	}

	@Override
	public void updateCollectCountByPk(String pkValue, boolean flag) {
		if (flag) {
			this.scenicDaoImpl.incrementCounterByPk(this.scenicId, SCENIC.collectCount);
		} else {
			this.scenicDaoImpl.decrementCounterByPk(this.scenicId, SCENIC.collectCount);

		}

	}

	@Override
	public void updateCollectCount(Scenic t, boolean flag) {

	}

	@Override
	public Scenic save(Scenic result) {
		// TODO Auto-generated method stub
		return null;
	}


	@Override
	public List<Scenic> search(String keywords) {
		List<Scenic> record = new ArrayList<>(1);
		BSONObject order = new BasicBSONObject();
		order.put("titleSize",1);
		BSONObject selector=null;
		try {
			selector = BasicBSONObject.typeToBson(new BasicBSONObject());
		} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException | IntrospectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		if (keywords != null && !keywords.isEmpty()) {
			BSONObject regex = new BasicBSONObject();
			regex.put(MatchConst._REGEX, keywords);
			regex.put(MatchConst._OPTIONS, "i");
			BasicBSONList arr = new BasicBSONList();
			BSONObject parmasOne = new BasicBSONObject();
			parmasOne.put("pinyin", regex);
			arr.add(parmasOne);
			BSONObject parmasTwo = new BasicBSONObject();
			parmasTwo.put("title", regex);
			arr.add(parmasTwo);
			BSONObject query = new BasicBSONObject();
			query.put(MatchConst._OR, arr);
			record = this.scenicDaoImpl.findAll(query, selector, order);
		}
		return record;
	}
	public  List<Scenic>  nearBy(Coordinates coordinates){
		List<String> neighbor = GeoHash.neighbor(coordinates.getLatitude(), coordinates.getLongitude());
		BasicBSONList list = new BasicBSONList();
		for (String geohash : neighbor) {
			BSONObject matcher = new BasicBSONObject();
			matcher.put(MatchConst._REGEX, geohash);
			BSONObject query = new BasicBSONObject();
			query.put(SCENIC.geohash, matcher);
			list.add(query);
		}
		BSONObject query = new BasicBSONObject();
		query.put(MatchConst._OR, list);
		BSONObject selector = new BasicBSONObject();
		BasicScenic basicScenic = new BasicScenic();
			try {
				selector = BasicBSONObject.typeToBson(basicScenic);
			} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException
					| IntrospectionException e) {
			
				e.printStackTrace();
			}
			return  this.scenicDaoImpl.findAll(query, selector, null);
	
}

	@Override
	public Scenic findOneByPk(String pkValue, Class<?> clazz) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Scenic findOneByPk(String pkValue) {
		// TODO Auto-generated method stub
		return null;
	}
}